package chapter06;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(urlPatterns = "/UserLoginDemo")
public class UserLoginDemo extends HttpServlet{
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = new User();
        try {
//            // 获取请求参数Map
//            Map<String, String[]> parameterMap = req.getParameterMap();
            // 将表单数据封装到User对象中
            BeanUtils.populate(user, req.getParameterMap());
            req.setAttribute("loginUser", user);
            // 请求转发到loginResult.jsp进行查询响应
            req.getRequestDispatcher("/loginResult.jsp").forward(req, resp);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
